$(document).ready(function() {
    var partyActivityId = getUrlParms("partyActivityId");

    $("#partyActivityName").blur(function (){
        if ($("#partyActivityName").val().length==0) {
            $("#isNull").html("该项不能为空！").css("color","red");
        }else{
            $("#isNull").html("");
        }
    });
    $.ajax({
        url: "http://localhost:8080/partyActivity/queryPartyNameByPAI",
        type: "GET",
        async: true,
        data: "partyActivityId=" + partyActivityId,
        dataType: "json",
        success: function (data) {
            //console.log(data)
            var partyName=data.data.partyName;
            $("#partyName").val(partyName);
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
    $.ajax({
        url: "http://localhost:8080/partyActivity/queryPartyActivityById",
        type: "GET",
        async: true,
        data: "partyActivityId=" + partyActivityId,
        dataType: "json",
        success: function (data) {
            //console.log(data)
            var partyActivity=data.data.partyActivity;

            $("#partyActivityId").val(partyActivity.partyActivityId);
            $("#partyActivityName").val(partyActivity.partyActivityName);
            $("#partyActivityTime").val(partyActivity.partyActivityTime);
            $("#partyActivityDescription").val(partyActivity.partyActivityDescription);
            var temp = partyActivity.partyActivityDescription;//3
            temp = temp.replace("&lt;p&gt;", "").replace("&lt;/p&gt;", "");//3
            tinyMCE.activeEditor.setContent(temp);
            $("#mce_23").remove();//2




        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });

});
//保存
$(document).on('click', '#save-btn', function() {

    var partyActivityId = getUrlParms("partyActivityId");
    var $partyActivityName= $("#partyActivityName").val();
    var $partyActivityTime=getDate($("#partyActivityTime").val());
    var $partyActivityDescription=html_encode(tinyMCE.activeEditor.getContent());

    console.log("$partyActivityDescription="+$partyActivityDescription);
    if ($partyActivityName===""){
        layer.msg("请输入活动名称");
        return;
    }
    //封装成对象提交
    var data = {
        partyActivityId:partyActivityId,
        partyActivityName:$partyActivityName,
        partyActivityTime:$partyActivityTime,
        partyActivityDescription:$partyActivityDescription,
    };
    $.ajax({
        url: "http://localhost:8080/partyActivity/savePartyActivity",
        type: "POST",
        async: false,
        data:JSON.stringify(data),
        dataType: "json",
        contentType:"application/json",
        success:function () {
            layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            layer.alert('保存成功!', function(){
                var index = parent.layer.getFrameIndex(window.name);
                setTimeout(function(){
                    parent.layer.close(index)
                }, 2000);
                parent.location.reload()
            });
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
});
//提交
$(document).on('click', '#update-btn', function() {

    var partyActivityId = getUrlParms("partyActivityId");
    var $partyActivityName= $("#partyActivityName").val();
    var $partyActivityTime=getDate($("#partyActivityTime").val());
    var $partyActivityDescription=html_encode(tinyMCE.activeEditor.getContent());


    if ($partyActivityName===""){
        layer.msg("请输入活动名称");
        return;
    }
    console.log("$partyActivityDescription="+$partyActivityDescription);
    //封装成对象提交
    var data = {
        partyActivityId:partyActivityId,
        partyActivityName:$partyActivityName,
        partyActivityTime:$partyActivityTime,
        partyActivityDescription:$partyActivityDescription,
    };
    $.ajax({
        url: "http://localhost:8080/partyActivity/commitPartyActivity",
        type: "POST",
        async: false,
        data:JSON.stringify(data),
        dataType: "json",
        contentType:"application/json",
        success:function () {
            layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            layer.alert('提交成功!', function(){
                var index = parent.layer.getFrameIndex(window.name);
                setTimeout(function(){
                    parent.layer.close(index)
                }, 2000);
                parent.location.reload()
            });
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
});



function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return unescape(r[2]);
    return null;
}

function getDate(str) {
    if(str.indexOf("-") >= 0 ){
        return str;
    }else {
        var arr=str.split(" ");
        console.log(arr[1]);
        var day=arr[0];
        var month=arr[1];
        var year=arr[2];
        if(month=='January'){
            month="01";
        }else if(month=='February'){
            month="02";
        }else if(month=='March'){
            month="03";
        }else if(month=='April'){
            month="04";
        }else if(month=='May'){
            month="05";
        }else if(month=='June'){
            month="06";
        }else if(month=='July'){
            month="07";
        }else if(month=='August'){
            month="08";
        }else if(month=='September'){
            month="09";
        }else if(month=='October'){
            month="10";
        }else if(month=='November'){
            month="11";
        }else if(month=='December'){
            month="12";
        }
        return year+"-"+month+"-"+day;
    }
}

/*html转码*/
function html_encode(str) {
    var s = "";
    if (str.length == 0)
        return "";
    s = str.replace(/&/g, "&gt;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/ /g, "&nbsp;");
    s = s.replace(/\'/g, "&#39;");
    s = s.replace(/\"/g, "&quot;");
    s = s.replace(/\n/g, "<br>");
    return s;
}